(function () {

    //获取显示页面数据 服务器分页
    var serverData = {};
    //serverData.superior=jQuery('#superior').val();
    serverData.key="";
    var serverTable;
    jQuery(document).ready(function () {
        serverTable = jQuery('#pumpTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/priceBook/singlePump/getpage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [
                {
                    data: null,
                    defaultContent: ""//排序
                },
                {
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                        var id=data.id
                        var operate ='<input type="checkbox" class="checkchildPump">';
                        return operate;
                    }
                },
                {
                    data: 'device.deviceType',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 25) ? '<font title="'+data+'">'+data.substring(0,25)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'device.deviceModel',
	                render:function(data,type,full,meta){
	                	if(data == null || data == undefined || data == ""){
							return data;
						}else{
							return (data.length > 25) ? '<font title="'+data+'">'+data.substring(0,25)+'...'+'</font>' : data;
						}
					}
                },
                {
                    data: 'superior'
                },
                {
                    data: 'price'
                }
            ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
        //删除单泵
        jQuery('.delBtn').on('click', function () {
            var selects=getSelect("#pumpTable",1);
            var url=window.rootPath+"/ui/priceBook/singlePump/delSinglePump";
            delSelect(selects,url,serverTable)
        });
        
        //编辑主机价格信息
        jQuery('.editBtn').on('click', function () {
            var selects=getSelect("#pumpTable",1);
            if(selects.length>1){
                $.springLemonDialog({
                    content: '只能编辑一条记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }
            else if(selects.length==0){
                $.springLemonDialog({
                    content: '请选择要编辑的记录',// 确认文本
                    clickButton: function (sender, modal, index) {// 按钮事件处理
                        $(this).closeDialog(modal);
                    }
                });
            }else if(selects.length==1) {
                jQuery('#id').val(selects[0].id);
                jQuery('#deviceId').val(selects[0].device.id);
	            jQuery('#deviceModel').val(selects[0].device.deviceModel);
	            jQuery('#deviceType').val(selects[0].device.deviceType);
	            jQuery('#superior').val(selects[0].superior);
	            jQuery('#createTime').val(selects[0].createTime);
	            jQuery('#price').val(selects[0].price);
	            jQuery('#pumpModal').modal('show');
            };
        });
        //列表中的全选
        $("#checkAllPump").click(function(){
            var check = $(this).prop("checked");
            $(".checkchildPump").prop("checked", check);
        });
        //保存价格
        jQuery('.saveBtn').on('click', function () {
            if(jQuery("#pumpEditForm") && !jQuery("#pumpEditForm").Validform({tiptype:3, showAllError:true}).check(false)){
                return;
            };
            var id=jQuery("#id").val();
            var deviceId=jQuery("#deviceId").val();
            var superior=jQuery('#superior').val();
            var price=jQuery('#price').val();
            var createTime=jQuery('#createTime').val()
            var data={
            	  "id":id,
                  "device":{
                	 "id":deviceId
                  },
                  "createTime":createTime,
                  "superior":superior,
                  "price":price
              };
            var url = window.rootPath + "/ui/priceBook/singlePump/savePrice";
            jQuery.ajax({
                  url:encodeURI(url),
                  type:'post',
                  data:JSON.stringify(data),
                  dataType:'json',
                  contentType:'application/json;charset=utf-8',
                  success: function () {
                      jQuery('#pumpModal').modal('hide');
                      serverTable.draw(false);
                  }
              });
        });

        //查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#keyword').val();
            serverTable.draw();
        })
        //撤销查询
        jQuery('.resetBtn').on("click",function()
        {
            jQuery('#keyword').val('');
            serverData.key ='';
            serverTable.draw();
        });
        //EXCEL导入
        jQuery('.btnImport').on("click",function()
        {
            fileUpload();
        });

        /*  //导入完成后刷新
         jQuery('.completeUploadBtn').on("click",function()
         {
         window.location.href=root+"/ui/device";
         });*/
        /**
         * 文件上传插件
         */
        var fileUpload = function() {
            var root = window.rootPath;
            WebFilesUploader(
                {
                    _webuploader : {
                        server : root + '/ui/priceBook/singlePump/import',
                        swf : root
                        + '/resources/poweradminjs/webupload/webfilesuploader/Uploader.swf',
                        chunked:false
                    }

                }, function(jsons) {
                    window.location.href=root+'/ui/priceBook/singlePump';
                });


        };
    });
})();

